//
//  DECXConfirmOutStockViewController.m
//  DECXInStore
//
//  Created by 李坤 on 2019/3/19.
//  Copyright © 2019年 程强. All rights reserved.
//

#import "DECXConfirmOutStockViewController.h"
#import "DECXManager.h"
#import "DECXDeliveryOrder.h"
#import "DECXDriverShowViewController.h"

@interface DECXConfirmOutStockViewController ()
@property (strong, nonatomic) DECXManager *manager;
@property (weak, nonatomic) IBOutlet UILabel *getID;
@property (weak, nonatomic) IBOutlet UILabel *category;
@property (weak, nonatomic) IBOutlet UILabel *getWeight;
@property (weak, nonatomic) IBOutlet UILabel *drivers;
@property (strong, nonatomic) DECXDeliveryOrder *dOrder;
@end

@implementation DECXConfirmOutStockViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setRightBarItem];
    self.manager = [DECXManager new];
    [self fetchInfo];
}
- (void)setRightBarItem
{
    UIBarButtonItem *item = [[UIBarButtonItem alloc]initWithTitle:@"确认出库" style:UIBarButtonItemStylePlain target:self action:@selector(confirmClick)];
    self.navigationItem.rightBarButtonItem = item;
}
- (void)confirmClick
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"您确定要出库吗？" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self confirmOut];
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alert addAction:action1];
    [alert addAction:action2];
    [self presentViewController:alert animated:YES completion:nil];
}
- (void)fetchInfo
{
    MyUser;
    weak_Self(self);
    [[self.manager scanGetDeliveryOrderByGetListID:self.getListID uid:user.userid depotID:user.depot_id]subscribeNext:^(id x) {
        DECXDeliveryOrder *order = x;
        weakSelf.getID.text = order.get_id;
        weakSelf.category.text = order.category_name;
        weakSelf.getWeight.text = [NSString stringWithFormat:@"%@吨",order.get_weight];
        if (order.driver.count == 0) {
            weakSelf.drivers.text = @"无";
        }else{
            weakSelf.drivers.text = [NSString stringWithFormat:@"%ld人",order.driver.count];
        }
        weakSelf.dOrder = x;
    } error:^(NSError *error) {
        
    } completed:^{
        
    }];
}
- (void)confirmOut
{
    weak_Self(self);
    [[self.manager confirmOutOfStock:self.getListID]subscribeNext:^(id x) {
        [weakSelf showMessageHUD:@"确认出库成功！" delayHide:1.0];
        [weakSelf performBlock:^{
            [weakSelf.navigationController popViewControllerAnimated:YES];
        } delay:1.0];
    } error:^(NSError *error) {
        [weakSelf showMessageHUD:@"确认出库失败！" delayHide:1.0];
    } completed:^{
        
    }];
}
- (IBAction)showDrivers:(id)sender
{
    DECXDriverShowViewController *vc = [[DECXDriverShowViewController alloc]init];
    vc.drivers = self.dOrder.driver;
    [self.navigationController pushViewController:vc animated:YES];
}
@end
